<template>
	<div id="schoolPhoto">
		<index>
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>管理</el-breadcrumb-item>
				<el-breadcrumb-item><a style="color: #000000;">微官网/班牌管理</a></el-breadcrumb-item>
			</el-breadcrumb>
			<div class="nav-tab-bar-box u-f-item u-f-jsb">
				<div class="nav-tab-bar u-f-item">
					<div
						class="font_14 nav-tab-item"
						@click="twacherTap(index)"
						:class="index == teacherIndex ? 'active' : ''"
						v-for="(item, index) in teacherMeun"
						:key="index"
					>
						{{ item.name }}
					</div>
				</div>
			</div>
			<div class="content">
				<div class="u-f">
					<div class="left">
						<div class="add u-f-justify el-icon-plus" @click="openDialog(1)"></div>
						<div class="building-list">
							<div v-for="(items, index) in PhotoTypeList" :label="items.name" :key="index">
								<div class="u-f-item item u-f-jsb" style="position: relative;" @click="leftTap(index,items.id)" :class="index == leftIndex ? 'active' : ''" @mouseover="items.isbtn = true"
										@mouseleave="items.isbtn = false">
									<div class="u-f-item">
										<div style="cursor: pointer;">{{ items.name }}</div>
									</div>
									<div class="u-f-item btn" v-if="items.isbtn" style="position: absolute;right: 10px;">
										<i class="el-icon-edit-outline u-f-justify" @click.prevent @click.stop="openDialog(2, items)"></i>
										<i class="el-icon-delete u-f-justify" @click.prevent @click.stop="delPlace(items.id)"></i>
									</div>
								</div>
							</div>
						</div>
					</div>
					<div class="right">
						<div class="title u-f-item u-f-jsb">
							<div>共{{ total }}条数据</div>
							<el-button size="small" @click="photoVisible=true">添加相片</el-button>
						</div>
						<el-table :data="ArticleList" height="83%" ref="multipleTable" tooltip-effect="dark" style="width: 100%;border: 1px solid #EEEEEE;">
							<!-- <el-table-column type="selection" width="55" align="center"></el-table-column> -->
							<el-table-column label="序号" type="index" width="65" align="center"></el-table-column>
							<el-table-column prop="title" label="标题" align="center"></el-table-column>
							<el-table-column label="图片" align="center">
								<template slot-scope="scope">
									<el-image
										fit="cover"
									    style="width: 50px; height: 50px"
									    :src="scope.row.file_text" 
									    :preview-src-list="[scope.row.file_text]">
									</el-image>
								</template>
							</el-table-column>
							<el-table-column prop="addtime" label="创建时间" align="center">
								<template slot-scope="scope">
									<div>{{$common.timeToDate(scope.row.createtime)}}</div>
								</template>
							</el-table-column>
							<el-table-column label="操作" align="center">
								<template slot-scope="scope">
									<el-button @click="editArticle(scope.row)" type="text" size="small">编辑</el-button>
									<el-button @click="delcourse(scope.row.id)" type="text" size="small" style="color:red;">删除</el-button>
								</template>
							</el-table-column>
						</el-table>
						<div class="u-f-right" style="padding: 15px 0;">
							<!-- <el-button type="danger" plain size="mini" @click="delUser">删除</el-button> -->
							<el-pagination
								@size-change="handleSizeChange"
								@current-change="handleCurrentChange"
								:current-page.sync="page"
								:page-size="limit"
								layout="total, prev, pager, next"
								:total="total"
							></el-pagination>
						</div>
					</div>
				</div>
			</div>
			

			<!-- 编辑 -->
			<el-dialog title="编辑分类" :visible.sync="editVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							分类名称
						</div>
						<el-input v-model="name" placeholder="请输入分类名称" style="width: 70%;"></el-input>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="editVisible = false" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="submit(2)">确 定</el-button>
				</span>
			</el-dialog>
			<!-- 添加 -->
			<el-dialog title="添加分类" :visible.sync="addVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							分类名称
						</div>
						<el-input v-model="name" placeholder="请输入分类名称" style="width: 70%;"></el-input>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="addVisible = false" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="submit(1)">确 定</el-button>
				</span>
			</el-dialog>
			<!-- 添加相片 -->
			<el-dialog title="添加相片" :visible.sync="photoVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							标题
						</div>
						<el-input v-model="title" placeholder="请输入标题" style="width: 70%;"></el-input>
					</div>
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							分类
						</div>
						<el-select v-model="classify_id" clearable placeholder="请选择分类" style="width: 70%;">
							<el-option v-for="item in PhotoTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
					<div class="u-f input-box">
						<div class="input-lebal">
							<span>*</span>图片
						</div>
						<el-upload
							class="avatar-uploader"
							action="http://school.blzzxx.cn/v1/school_api/common/upload"
							:show-file-list="false"
							:on-success="handleAvatarSuccess"
							:before-upload="beforeAvatarUpload"
							:headers="headers"
						>
							<img v-if="imageUrl" :src="imageUrl" class="avatar" />
							<i v-else class="el-icon-plus avatar-uploader-icon"></i>
						</el-upload>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="quxiao" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="addPhoto(1)">确 定</el-button>
				</span>
			</el-dialog>
			<!-- 添加相片 -->
			<el-dialog title="编辑相片" :visible.sync="editphotoVisible" width="500px" :before-close="handleClose">
				<div class="edit-box">
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							标题
						</div>
						<el-input v-model="title" placeholder="请输入标题" style="width: 70%;"></el-input>
					</div>
					<div class="u-f-item input-box">
						<div class="input-lebal">
							<span>*</span>
							分类
						</div>
						<el-select v-model="classify_id" clearable placeholder="请选择分类" style="width: 70%;">
							<el-option v-for="item in PhotoTypeList" :key="item.id" :label="item.name" :value="item.id"></el-option>
						</el-select>
					</div>
					<div class="u-f input-box">
						<div class="input-lebal">
							<span>*</span>图片
						</div>
						<el-upload
							class="avatar-uploader"
							action="http://school.blzzxx.cn/v1/school_api/common/upload"
							:show-file-list="false"
							:on-success="handleAvatarSuccess"
							:before-upload="beforeAvatarUpload"
							:headers="headers"
						>
							<img v-if="imageUrl" :src="imageUrl" class="avatar" />
							<i v-else class="el-icon-plus avatar-uploader-icon"></i>
						</el-upload>
					</div>
				</div>
				<span slot="footer" class="dialog-footer u-f-item u-f-jsb">
					<el-button @click="quxiao" size="small">取 消</el-button>
					<el-button type="primary" size="small" @click="addPhoto(2)">确 定</el-button>
				</span>
			</el-dialog>
		</index>
	</div>
</template>

<script>
import index from '@/components/common/index.vue';
export default {
	components: {
		index
	},
	data() {
		return {
			teacherMeun: [
				{ name: '资讯管理',id:1},
				{ name: '轮播图管理',id:2},
				{ name: '校园相册',id:3},
				{ name: '校园视频',id:4},
				{ name: '班牌账号',id:5}
			],
			teacherIndex: 2,
			PhotoTypeList:[],
			addVisible:false,
			name:'',
			editVisible:false,
			headers:{
				token:window.localStorage.getItem("token")
			},
			imageUrl: '',
			image:'',
			action:'/common/upload',
			total:0,
			leftIndex:0,
			page:1,
			limit:10,
			ArticleList:[],
			type:2,
			photoVisible:false,
			editphotoVisible:false,
			title:'',
			photoId:'',
			classify_id:''
		};
	},
	mounted() {
		this.getSchoolPhotoType();
	},
	methods: {
		toAdd(){
			this.$router.push('/addArticle')
		},
		handleAvatarSuccess(res, file) {
			if(res.code==1){
				this.imageUrl = res.data.fullurl;
				this.image = res.data.url;
			}else{
				this.$message.error(res.msg)
			}
			
		},
		beforeAvatarUpload(file) {
			console.log(file);
		},
		twacherTap(index){
			this.teacherIndex = index;
			if(index==0){
				this.$router.push('/official')
			}else if(index==1){
				this.$router.push('/setBanner')
			}else if(index==2){
				this.$router.push('/schoolPhoto')
			}else if(index==3){
				this.$router.push('/schoolVideo')
			}else if(index==4){
				this.$router.push('/account')
			}
			
		},
		// 获取资讯分类列表
		getSchoolPhotoType(){
			this.$api.setting.schoolPhotoType({
				page:1
			}).then(res=>{
				if(res.data.code==1){
					let data = res.data.data.rows;
					for (let i in data) {
						data[i].isbtn = false;
					}
					this.PhotoTypeList = data;
					this.getSchoolPhotoList()
				}
			})
		},
		leftTap(index,id) {
			this.leftIndex = index;
			this.page = 1;
			this.getSchoolPhotoList()
		},
		// 初始化表单
		initialize() {
			this.name = '';
			this.id = '';
			this.title = '';
			this.classify_id = '';
			this.image = '';
			this.imageUrl = '';
			this.photoId = '';
		},
		// 关闭弹窗
		handleClose(done) {
			this.editVisible = false;
			this.addVisible = false;
			this.initialize();
			done();
		},
		quxiao(){
			this.photoVisible = false;
			this.editphotoVisible = false;
			this.initialize();
		},
		handleSizeChange(val) {
			console.log(`每页 ${val} 条`);
		},
		handleCurrentChange(val) {
			this.page = val;
			this.getSchoolPhotoList();
		},
		openDialog(type, item) {
			if (type == 1) {
				this.addVisible = true;
			} else {
				this.editVisible = true;
				this.id = item.id;
				this.name = item.name;
			}
		},
		submit(type) {
			let api = this.$api.setting.addSchoolPhotoType;
			let data = {
				name: this.name
			};
			if (type == 2) {
				data.id = this.id;
				api = this.$api.setting.editSchoolPhotoType;
			}
			api(data).then(res => {
				if (res.data.code == 1) {
					if (type == 1) {
						this.$message.success('添加成功');
					} else {
						this.$message.success('修改成功');
					}
					this.addVisible = false;
					this.editVisible = false;
					this.initialize();
					this.getSchoolPhotoType();
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		delPlace(id) {
			let _this = this;
			_this
				.$confirm('确定要删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消'
				})
				.then(() => {
					_this.$api.setting
						.delSchoolPhotoType({
							id: id
						})
						.then(res => {
							if (res.data.code == 1) {
								_this.$message.success('删除成功');
								_this.getSchoolPhotoType();
							} else {
								_this.$message.error(res.data.msg);
							}
						});
				});
		},
		// 获取相片列表
		getSchoolPhotoList(){
			let data = {
				page:this.page,
				pagesize:this.limit,
				filter:JSON.stringify({classify_id:this.PhotoTypeList[this.leftIndex].id})
			}
			this.$api.setting.schoolPhotoList(data).then(res => {
				if (res.data.code == 1) {
					this.ArticleList = res.data.data.rows;
					this.total = res.data.data.total;
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		// 添加相片
		addPhoto(type){
			let api = this.$api.setting.addSchoolPhoto;
			let data = {
				title: this.title,
				classify_id:this.classify_id,
				file:this.image
			};
			if (type == 2) {
				data.id = this.photoId;
				api = this.$api.setting.editSchoolPhoto;
			}
			api(data).then(res => {
				if (res.data.code == 1) {
					if (type == 1) {
						this.$message.success('添加成功');
					} else {
						this.$message.success('修改成功');
					}
					this.photoVisible = false;
					this.editphotoVisible = false;
					this.initialize();
					this.getSchoolPhotoType();
				} else {
					this.$message.error(res.data.msg);
				}
			});
		},
		// 删除资讯
		delcourse(id){
			let _this = this;
			_this.$confirm('确定要删除？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消'
				})
				.then(() => {
					_this.$api.setting
						.delSchoolPhoto({
							id: id
						})
						.then(res => {
							if (res.data.code == 1) {
								_this.$message.success('删除成功');
								_this.page = 1;
								_this.getSchoolPhotoList();
							} else {
								_this.$message.error(res.data.msg);
							}
						});
				});
		},
		editArticle(e){
			this.photoId = e.id;
			this.title = e.title;
			this.classify_id = e.classify_id;
			this.imageUrl = e.file_text;
			this.image = e.file;
			this.editphotoVisible = true;
		}
	}
};
</script>

<style lang="scss">
#schoolPhoto {
	.nav-tab-bar-box {
		background-color: #ffffff;
		padding: 15px;
		margin-top: 1.04vw;
		.nav-tab-bar {
			.nav-tab-item {
				color: #6e7a87;
				padding: 0 0.78125vw;
				border-right: 1px solid #f3f3f3;
				cursor: pointer;
				-webkit-touch-callout: none;
				-webkit-user-select: none;
				-khtml-user-select: none;
				-moz-user-select: none;
				-ms-user-select: none;
				user-select: none;
			}
			&div:last-child {
				border-right: none;
			}
			.active {
				font-size: 0.833vw;
				color: #63acff;
				position: relative;
				font-weight: bold;
			}
			.active::after {
				position: absolute;
				content: '';
				height: 2px;
				left: 0.78125vw;
				right: 0.78125vw;
				background-color: #63acff;
				bottom: -0.78125vw;
			}
		}
	}
	.head {
		background-color: #ffffff;
		padding: 10px 30px;
		margin-top: 15px;
		.span {
			margin-right: 10px;
			background-color: #f9f9f9;
			color: #8d9da6;
			padding: 3px 10px;
		}
	}
	.content {
		margin-top: 20px;
		background-color: #ffffff;
		height: 78vh;
		.left {
			border-right: 1px solid #eeeeee;
			height: 78vh;
			min-width: 300px;
			.add {
				background-color: #f8f8f8;
				padding: 10px 0;
				font-size: 24px;
				color: #8d9da6;
				cursor: pointer;
			}
			.add:hover {
				color: #007aff;
			}
			.building-list {
				padding: 25px;
				height: 70vh;
				overflow-y: auto;
				.item {
					padding: 0 15px;
					height: 40px;
					line-height: 40px;
					margin-top: 15px;
					background-color: #f8f8f8;
					.el-icon-caret-right,
					.el-icon-caret-bottom {
						color: #a4a4a4;
						margin-right: 5px;
						font-size: 16px;
					}
					.el-checkbox {
						display: block;
					}
					.btn {
						i {
							background-color: #e8eaec;
							color: #a4a4a4;
							width: 25px;
							height: 25px;
							margin-left: 10px;
							border-radius: 50%;
						}
						i:hover {
							color: red;
						}
					}
				}
				.active {
					background-color: #e4f1ff;
					color: #007aff;
				}
				.floor-list {
					background-color: #f8f8f8;
					border-top: 1px solid #eeeeee;
					padding: 0 15px 15px 30px;
					.floor-item {
						background-color: #f1f1f1;
						padding: 0 15px 0 25px;
						color: #4c5559;
						height: 40px;
						line-height: 40px;
						margin-top: 15px;
						.btn {
							i {
								background-color: #ffffff;
								color: #a4a4a4;
								width: 25px;
								height: 25px;
								margin-left: 10px;
								border-radius: 50%;
							}
							i:hover {
								color: red;
							}
						}
					}
					.active {
						background-color: #e4f1ff;
						color: #007aff;
					}
				}
			}
		}
		.right {
			height: 78vh;
			width: 100%;
			padding: 25px 50px 0 50px;
			.title {
				padding: 0 0 15px 0;
			}
		}
	}

	.el-table th > .cell,
	.el-table .cell {
		overflow: hidden; /*超出部分隐藏*/
		white-space: nowrap; /*不换行*/
		text-overflow: ellipsis; /*超出部分文字以...显示*/
	}
	.el-table thead {
		background: #fafafa !important;
	}
	.el-table th {
		padding: 15px 5px !important;
		background: #fafafa !important;
	}
	.el-table::before {
		left: 0;
		bottom: 0;
		width: 100%;
		height: 0;
	}

	.el-dialog__header {
		border-bottom: 1px solid #eeeeee;
		padding: 10px 20px;
		.el-dialog__title {
			font-size: 16px;
		}
	}
	.el-dialog__body {
		padding: 0 20px 35px 20px;
		.edit-box {
			.input-box {
				margin-top: 35px;
				padding-left: 15px;
				.input-lebal {
					width: 20%;
					text-align: right;
					color: #aeb5b8;
					margin-right: 10px;
					span {
						color: #ff7a65;
						margin-right: 3px;
					}
				}
				.el-icon-remove-outline {
					font-size: 1.5625vw;
					color: #d7d9db;
					margin-left: 10px;
				}
			}
			.upload-file {
				position: absolute;
				left: 0;
				right: 0;
				top: 0;
				bottom: 0;
				opacity: 0;
				cursor: pointer;
			}
			.avatar-uploader .el-upload {
				border: 1px dashed #d9d9d9;
				border-radius: 6px;
				cursor: pointer;
				position: relative;
				overflow: hidden;
			}
			.avatar-uploader .el-upload:hover {
				border-color: #409eff;
			}
			.avatar-uploader-icon {
				font-size: 28px;
				color: #8c939d;
				width: 150px;
				height: 150px;
				line-height:150px;
				text-align: center;
			}
			.avatar {
				width: 150px;
				height: 150px;
				display: block;
			}
		}
	}
	.el-dialog__footer {
		padding: 15px 20px 15px;
		box-shadow: 0px -5px 10px -5px #eeeeee;
	}
}
</style>
